package com.jyu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyu.dao.PositionDao;
import com.jyu.domain.Position;
import com.jyu.service.PositionService;
import javafx.geometry.Pos;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Service
public class PositionServiceImpl extends ServiceImpl<PositionDao, Position> implements PositionService {

    @Autowired
    private PositionDao positionDao;

    @Autowired
    private RestHighLevelClient client;

    @Override
    public boolean savePosition(Position position) {
        return positionDao.insert(position) > 0;
    }

    @Override
    public boolean modify(Position position) {
        return positionDao.updateById(position) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return positionDao.deleteById(id) > 0;
    }

    @Override
    public IPage<Position> getPage(int currentPage, int pageSize, Position position) {

        //用于Lambda语法使用的查询Wrapper 条件构造器
        LambdaQueryWrapper<Position> lqw = new LambdaQueryWrapper<Position>();
        lqw.like(Strings.isNotEmpty(position.getJobTitle()), Position::getJobTitle, position.getJobTitle());
        lqw.like(Strings.isNotEmpty(position.getCompanyName()), Position::getCompanyName, position.getCompanyName());
        lqw.like(Strings.isNotEmpty(position.getWorkplace()), Position::getWorkplace, position.getWorkplace());
        lqw.orderByDesc(Position::getUpdateTime, Position::getId);
        IPage page = new Page(currentPage, pageSize);
        positionDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public List<String> getField() {
        List<String> field = positionDao.getField();
        return field;
    }

    @Override
    public void addPositionDocument(Integer id) {
        try {
            Position position = this.getById(id);
            String salary = position.getSalary();
            if("面议".equals(salary))
            {
            }else if(salary.contains("/月")){
                int i = salary.indexOf("/月");
                String substring = salary.substring(0, i);
                String[] split = substring.split("-");
                position.setMinSalary(Integer.valueOf(split[0]));
                position.setMaxSalary(Integer.valueOf(split[1]));
                position.setSalary(null);
                System.out.println(position);
            }
            IndexRequest request=new IndexRequest("position").id(id.toString()).source(JSON.toJSONString(position), XContentType.JSON);
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deletePositionDocument(Integer id) {
        try {
            DeleteRequest request=new DeleteRequest("position",id.toString());
            client.delete(request,RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
